/*
===============================================================================
 Name        : main.c
 Author      : 
 Version     :
 Copyright   : (C) Copyright
 Description : main definition
===============================================================================
*/

#ifdef __USE_CMSIS
#include "LPC11xx.h"
#endif

/*using global define value */
#include "control.h"

/* using general pin io */
#include "gpio/gpio.h"

/* using touch function */
#include "touch-api/touch-api.h"

/* using timer */
#include "timer/timer32.h"

/* using lcd function */
#include "lcd-api/lcd-api.h"

/*using graphic*/
#include "graphic.h"


//#define RESET_PORT (0)
//#define RESET_PIN  (9)

/* button use */
#define BUTTON_PORT (3)
#define BUTTON_A    (4)
#define BUTTON_B    (5)

/*-------------- GUI use --------------*/
#define BACKGROUND_COLOR (0x0)
#define MENU_COLOR gray
#define MENU_X (0)
#define MENU_Y (0)
#define MENU_WIDTH (30)
#define MENU_HEIGHT (MAX_HEIGHT)
#define MENU_LINE_WIDTH (3)

/*lock/unlock button position*/
#define LOCK_BUTTON_X (5)
#define LOCK_BUTTON_Y (10)
#define LOCK_BUTTON_WIDTH (20)
#define LOCK_BUTTON_HEIGHT (20)
#define LOCK_BUTTON_COLOR1 (red)
#define LOCK_BUTTON_COLOR2 (blue)

/*reset button position*/
#define RESET_BUTTON_X (5)
#define RESET_BUTTON_Y (50)
#define RESET_BUTTON_WIDTH (20)
#define RESET_BUTTON_HEIGHT (60)
#define RESET_BUTTON_COLOR1 (red)
#define RESET_BUTTON_COLOR2 (blue)

/*front back back button position*/
#define FB_BUTTON_X (5)
#define FB_BUTTON_Y (140)
#define FB_BUTTON_WIDTH (20)
#define FB_BUTTON_HEIGHT (60)
#define FB_BUTTON_COLOR1 (red)
#define FB_BUTTON_COLOR2 (blue)

/* card index postion */
#define CARD_INDEX_X (12)
#define CARD_INDEX_Y (280)

/*main data use*/
struct {
	//card index (0..xxxx)
	uint32_t card_index;

	//lock / unlock screen
	int8_t card_lock;

	//card side (0=front,1=back)
    enum {
		FRONT,BACK
	}card_side;

	enum {
		MIN_CARD_INDEX=0,
		MAX_CARD_INDEX=9999
	}card_value;
}main_data_str;

/* draw posion function */
uint8_t draw_Position(uint32_t x,uint32_t y)
{
    //check index that less than 1xxxx
	if((x>9999)|(y>9999))
    	return false;

    uint16_t color=yellow;
    uint8_t rotate=0;
    uint8_t remain;

    //draw x
	remain=(x/1000);//1000
     DrawNumber(color,100,10,remain,BACKGROUND_COLOR,rotate);
	remain=((x%1000)/100);//100
	  DrawNumber(color,100+FONT_WIDTH+1,10,remain,BACKGROUND_COLOR,rotate);
	remain=((x%100)/10);//10
	  DrawNumber(color,100+(FONT_WIDTH*2)+2,10,remain,BACKGROUND_COLOR,rotate);
	remain=x%10;//1
      DrawNumber(color,100+(FONT_WIDTH*3)+3,10,remain,BACKGROUND_COLOR,rotate);

    //draw y
  	remain=(y/1000);//1000
       DrawNumber(color,150,10,remain,BACKGROUND_COLOR,rotate);
  	remain=((y%1000)/100);//100
  	  DrawNumber(color,150+FONT_WIDTH+1,10,remain,BACKGROUND_COLOR,rotate);
  	remain=((y%100)/10);//10
  	  DrawNumber(color,150+(FONT_WIDTH*2)+2,10,remain,BACKGROUND_COLOR,rotate);
  	remain=y%10;//1
        DrawNumber(color,150+(FONT_WIDTH*3)+3,10,remain,BACKGROUND_COLOR,rotate);


    return true;
}

/* draw card index function */
uint8_t draw_Card_Index(uint32_t index)
{
    //check index that less than 1xxxx
	if(index>9999)
    	return false;

    uint16_t color=yellow;
    uint8_t rotate=1;
    uint8_t remain;

	remain=(index/1000);//1000
     DrawNumber(color,CARD_INDEX_X,CARD_INDEX_Y,remain,BACKGROUND_COLOR,rotate);
	remain=((index%1000)/100);//100
	  DrawNumber(color,CARD_INDEX_X,CARD_INDEX_Y-FONT_HEIGHT-1,remain,BACKGROUND_COLOR,rotate);
	remain=((index%100)/10);//10
	  DrawNumber(color,CARD_INDEX_X,CARD_INDEX_Y-(FONT_HEIGHT*2)-2,remain,BACKGROUND_COLOR,rotate);
	remain=index%10;//1
      DrawNumber(color,CARD_INDEX_X,CARD_INDEX_Y-(FONT_HEIGHT*3)-3,remain,BACKGROUND_COLOR,rotate);
    return true;
}

/*Interrupt handle function for PIO3*/
void PIOINT3_IRQHandler(void){

	uint32_t regVal =0;

	//delay for sometime then read value solve bouncing problem
    Delayms(200);

	regVal=GPIOIntStatus(BUTTON_PORT,BUTTON_A );
    if ( regVal ) // this is interrupt from touch pen
    {
    	if(main_data_str.card_index>MIN_CARD_INDEX)
    	    main_data_str.card_index--;
    	else
    	   main_data_str.card_index=MAX_CARD_INDEX;
  		//clear interrupt
  		GPIOIntClear(BUTTON_PORT,BUTTON_A );
    }
    regVal=0;
    regVal=GPIOIntStatus(BUTTON_PORT,BUTTON_B );
    if ( regVal ) // this is interrupt from touch pen
	{
		 if(main_data_str.card_index<MAX_CARD_INDEX)
			 main_data_str.card_index++;
		 else
			 main_data_str.card_index=MIN_CARD_INDEX;
		//clear interrupt
		GPIOIntClear(BUTTON_PORT,BUTTON_B );
	}

    //draw card index
    draw_Card_Index(main_data_str.card_index);

}

int main(void) {
	
	/* Basic chip initialization is taken care of in SystemInit() called
	 * from the startup code. SystemInit() and chip settings are defined
	 * in the CMSIS system_<part family>.c file.
	 */

	/*init timer 1 that using PIO1_4,TIME_INTERVAL is defined as 10mS */
	init_timer32(TIMER1, TIME_INTERVAL);

	 /* Enable timer 1. Our interrupt handler will begin incrementing
	  * the TimeTick global each time timer 0 matches and resets.
	*/
	enable_timer32(TIMER1);

	GPIOInit();

	/*---------------Init Main Data -----------------*/
	main_data_str.card_index=0;
	main_data_str.card_side=FRONT;

	/*---------------Touch screen init ---------------*/
	init_Touch();

	/*------------------ RESET PIN -------------------*/
	//GPIOSetDir(RESET_PORT,RESET_PIN,OUTPUT);
	//GPIOIntDisable(RESET_PORT,RESET_PIN);

	/*------------------ BUTTON A B ------------------*/
	GPIOSetDir(BUTTON_PORT,BUTTON_A,INPUT);
	//set interrupt ,level ,double,low
	GPIOSetInterrupt( BUTTON_PORT,BUTTON_A, 1, 1, 0);
	GPIOIntEnable(BUTTON_PORT,BUTTON_A);
	GPIOSetDir(BUTTON_PORT,BUTTON_B,INPUT);
	GPIOSetInterrupt( BUTTON_PORT,BUTTON_B, 1, 1, 0);
	GPIOIntEnable(BUTTON_PORT,BUTTON_B);

	/*------------------LCD initial-------------------*/

		uint32_t temp=0;
		uint32_t i=0;

		//reset lcd first
		//GPIOSetValue(RESET_PORT,RESET_PIN,1);
		//Delayms(1);
		//GPIOSetValue(RESET_PORT,RESET_PIN,0);
		//Delayms(5);
		//GPIOSetValue(RESET_PORT,RESET_PIN,1);
		//Delayms(10);

		//init pin io and paint black screen
		init_LCD();


		//clear to black screen
		FillRectangle(BACKGROUND_COLOR,
				   0,0,
				   MAX_WIDTH,MAX_HEIGHT);

		//draw menu area
   		DrawRectangle(MENU_COLOR,
   					  MENU_X,MENU_Y,
   					  MENU_WIDTH,MENU_HEIGHT,
   					  MENU_LINE_WIDTH);

   		//draw reset button
		FillRectangle(RESET_BUTTON_COLOR1,
					  RESET_BUTTON_X,RESET_BUTTON_Y,
					  RESET_BUTTON_WIDTH,RESET_BUTTON_HEIGHT);

		//draw lock button
		FillRectangle(LOCK_BUTTON_COLOR1,
					  LOCK_BUTTON_X,LOCK_BUTTON_Y,
					  LOCK_BUTTON_WIDTH,LOCK_BUTTON_HEIGHT);

		//draw front/back button
		FillRectangle(FB_BUTTON_COLOR1,
				      FB_BUTTON_X,FB_BUTTON_Y,
				      FB_BUTTON_WIDTH,FB_BUTTON_HEIGHT);

#if 0
		//draw front image
		DrawImage(fb_pic1,
				  FB_BUTTON_X+10,FB_BUTTON_Y+20,
		   		  11,8,1);
#endif

		//draw card number
   		draw_Card_Index(main_data_str.card_index);

		//draw sharp image
		DrawImage(sharp_pic,
				  CARD_INDEX_X,CARD_INDEX_Y+9,
		   		   5,8,1);

   		//default is not lock the card
   		main_data_str.card_lock=false;

       	while(1)
        {


#if 0
       		DrawRectangle(colorc[temp],
       					   100+x,100+y,
       					   50,50,
       					   10);
#endif



       			//for ( i = 0; i < DELAY_COUNT; i++ );	/* delay minimum 250ns */
       	 if(get_TouchGlobalFlag()==true)
       	 {

       		uint16_t gx=get_Gx();
       		uint16_t gy=get_Gy();

       		//draw position of touch
       		//draw_Position(gx,gy);

       		//check lock button
       		if((gx>=LOCK_BUTTON_X)&&
			 (gx<=(LOCK_BUTTON_X+LOCK_BUTTON_WIDTH))&&
			 (gy>=LOCK_BUTTON_Y)&&
			 (gy<=LOCK_BUTTON_Y+LOCK_BUTTON_HEIGHT)){

				  //lock or unlock
				 if(main_data_str.card_lock==true){
				   main_data_str.card_lock=false;
				   FillRectangle(LOCK_BUTTON_COLOR1,
								 LOCK_BUTTON_X,LOCK_BUTTON_Y,
								 LOCK_BUTTON_WIDTH,LOCK_BUTTON_HEIGHT);

				 }else{
					 main_data_str.card_lock=true;
					 FillRectangle(LOCK_BUTTON_COLOR2,
										  LOCK_BUTTON_X,LOCK_BUTTON_Y,
										  LOCK_BUTTON_WIDTH,LOCK_BUTTON_HEIGHT);
				 }
				  //bouncing
				  Delayms(100);
       		}//end lock button click

       		if(main_data_str.card_lock==false){
       			    //draw_Card_Index(gy);
   			        //click on menu area
					if((gx>=MENU_X)&&
					   (gx<=(MENU_X+MENU_WIDTH))&&
					   (gy>=MENU_Y)&&
					   (gy<=MENU_Y+MENU_HEIGHT))
					{
							//click on front/back button
							if((gx>=FB_BUTTON_X)&&
							   (gx<=(FB_BUTTON_X+FB_BUTTON_WIDTH))&&
							   (gy>=FB_BUTTON_Y)&&
							   (gy<=FB_BUTTON_Y+FB_BUTTON_HEIGHT)){

								if(main_data_str.card_side==FRONT){
									main_data_str.card_side=BACK;
									FillRectangle(FB_BUTTON_COLOR1,
												   FB_BUTTON_X,FB_BUTTON_Y,
												   FB_BUTTON_WIDTH,FB_BUTTON_HEIGHT);
								}else{

									main_data_str.card_side=FRONT;
									FillRectangle(FB_BUTTON_COLOR1,
												  FB_BUTTON_X,FB_BUTTON_Y,
												  FB_BUTTON_WIDTH,FB_BUTTON_HEIGHT);
								}

								//bouncing
								Delay(5);
							}else if((gx>=RESET_BUTTON_X)&&
									 (gx<=(RESET_BUTTON_X+RESET_BUTTON_WIDTH))&&
									 (gy>=RESET_BUTTON_Y)&&
									 (gy<=RESET_BUTTON_Y+RESET_BUTTON_HEIGHT)){

									//draw reset button
									FillRectangle(RESET_BUTTON_COLOR2,
												  RESET_BUTTON_X,RESET_BUTTON_Y,
												  RESET_BUTTON_WIDTH,RESET_BUTTON_HEIGHT);
									//clear to black screen
									FillRectangle(BACKGROUND_COLOR,
													   MENU_WIDTH,0,
													   MAX_WIDTH-MENU_WIDTH,MAX_HEIGHT);
							   		//draw reset button
									FillRectangle(RESET_BUTTON_COLOR1,
												  RESET_BUTTON_X,RESET_BUTTON_Y,
												  RESET_BUTTON_WIDTH,RESET_BUTTON_HEIGHT);

								}//end reset button pressed
					}else{
						//draw cursor
						FillRectangle(0xFFFF,
									  gx,gy,
									  4,4);


					}
       		}//end if card doest not lock

    		/*reset TouchGlobalFlag value */
    		set_TouchGlobalFlag(false);

       	 }

       		//x+=temp;
       		//y+=50;
    		//Delayms(50);
    		//GPIOSetValue(0,7, 0 );
    		//Delay(3);
    		//i++;
                    //temp=i%9;

            /* Go to sleep to save power between timer interrupts */
           //__WFI();
         }
	return 0 ;
}
